package com.example.leetcode.trainingcamp.week12;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class Offer48 {

    public int lengthOfLongestSubstring(String s) {
        if (s.equals("")){
            return 0;
        }
        Set<Character> set = new HashSet<>();
        int left = 0;
        int right = 1;
        int length = s.length();
        int max = -1;
        set.add(s.charAt(left));
        while (right < length) {
            char c = s.charAt(right);
            if (set.contains(c)) {
                max = Math.max(right - left , max);
                set.remove(s.charAt(left));
                left++;
            } else {
                set.add(c);
                right++;
            }
        }
        max = Math.max(max,right - left );
        return max;
    }

    public static void main(String[] args) {
        Offer48 o = new Offer48();
        String s = "abcabcbb";
        System.out.println(o.lengthOfLongestSubstring(s));
    }
}
